package cn.edu.leetcode.查找;

public class 二分查找_递归算法 {

    public static void main(String[] args) {
        int[] srcArray = {1, 2, 3, 4, 5, 6};
        int des = 6;

        System.out.println(binarySearch(srcArray, des, 0, srcArray.length - 1));
    }

    public static int binarySearch(int[] dataset, int data, int beginIndex, int endIndex) {

        if (beginIndex > endIndex) {
            return -1;
        }

        int midIndex = (beginIndex + endIndex) / 2;
        if (data < dataset[midIndex]) {
            return binarySearch(dataset, data, beginIndex, midIndex - 1);
        } else if (data > dataset[midIndex]) {
            return binarySearch(dataset, data, midIndex + 1, endIndex);
        } else {
            return midIndex;
        }
    }
}
